package com.gogowan.agroenterprise.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import android.content.Context;
import android.os.Environment;

/**
 * 日志输出�?
 * @author zk
 * 
 */
public class MyLog {
	/**
	 * 文件路径
	 */
	public static String FILE = "LicensePlate";
	/**
	 * 文件�?
	 */
	private String LOG = "software.log";

	private Context context;
	/**
	 * 文件编码格式
	 */
	private String encoding = "GBK";
	
	/**
	 * 单一的堆�?
	 */
	private StackTraceElement[] stack=null;

	/**
	 * 构�?日志�?
	 * @param context
	 */
	public MyLog(Context context) {
		this.context = context;
	}
	/**
	 * 构�?日志�?
	 * @param context
	 * @param log 日志文本
	 */
	public MyLog(Context context,String log) {
		this.context = context;
		this.LOG=log;
	}
	/**
	 * 构�?日志�?
	 * @param context
	 * @param log 日志文本
	 * @param stack 单一的堆�?
	 */
	public MyLog(Context context,String log,StackTraceElement[] stack) {
		this.context = context;
		this.LOG=log;
		this.stack=stack;
	}
	/**
	 * 写入到手�?
	 * 
	 * @param tag
	 * @param message
	 */
	public void writeLog(String tag, String message) {
		try {
			String time = getTime();
			String read = read();
			if (read != null || !read.equals("")) {
				write(read + "\n" + time + "-[" + tag + "]" + message);
			}
		} catch (IOException e) {
			// TODO: handle exception
			try {
				write("NEW�" + getTime());// 新建文件
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 写入到SD�?
	 * 
	 * @param tag
	 * @param message
	 */
	public void writeSDCardLog(String tag, String message) {
		String read = null;
		String time = getTime();
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 表示sdcard存在，并且可以进行读�?
			try {
				// Environment.getExternalStorageDirectory();//获取sdcard根路�?
				read = readSDcard();
			} catch (IOException e) {
				// TODO: handle exception
				try {
					writeSDcard("NEW"+time,true);// 新建文件
					read = readSDcard();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
				}
			}
			try {
				if (read != null || !read.equals("")) {
					writeSDcard(read + "\n" + "[" + time + "-" + tag + "]"
							+ message,false);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
		}
	}

	/**
	 * 写入到文�?
	 * @param wirte 要写入的数据
	 * @throws IOException
	 */
	private void write(String wirte) throws IOException {
		try {
			FileOutputStream fos = context.openFileOutput(LOG,
					Context.MODE_PRIVATE);
			fos.write(wirte.getBytes());
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 读取日志
	 * @return 字符�?
	 * @throws IOException
	 */
	private String read() throws IOException {
		try {
			FileInputStream inStream = context.openFileInput(LOG);
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int length = -1;
			while ((length = inStream.read(buffer)) != -1) {
				stream.write(buffer, 0, length);
			}
			stream.close();
			inStream.close();
			return stream.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 * 写入到SD�?
	 * @param content 内容
	 * @param isNew 是否新建
	 * @throws IOException
	 */
	private void writeSDcard(String content,boolean isNew) throws IOException {
		File f = new File(Environment.getExternalStorageDirectory(), FILE);
		if (!f.exists()) {
			f.mkdirs();
		}
		File file = new File(Environment.getExternalStorageDirectory(), FILE
				+ "/" + LOG);
		FileOutputStream outStream = new FileOutputStream(file);
		outStream.write(content.getBytes(encoding));
		if (stack != null&&!isNew) {
			for (int i = 0; i < stack.length; i++) {
				String stackStr=stack[i].toString()+"\n";
				outStream.write(stackStr.getBytes(encoding));
			}
		}
		outStream.close();
	}

	/**
	 * 读取SD卡的日志
	 * @return 内容
	 * @throws IOException
	 */
	private String readSDcard() throws IOException {
		File file = new File(Environment.getExternalStorageDirectory(), FILE+"/"+LOG);
		FileInputStream inputStream = new FileInputStream(file);
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int len = -1;

		while ((len = inputStream.read(b)) != -1) {
			outStream.write(b, 0, len);

		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inputStream.close();
		return new String(data, encoding);
	}

	/**
	 * 得到当前日期
	 * @return 日期
	 */
	public String getTime() {
		final Calendar c = Calendar.getInstance();
		int mYear = c.get(Calendar.YEAR);
		String mMonth = Utils.formatTime(c.get(Calendar.MONTH) + 1);
		String mDay = Utils.formatTime(c.get(Calendar.DAY_OF_MONTH));
		String mHear = Utils.formatTime(c.get(Calendar.HOUR_OF_DAY));
		String mMinute = Utils.formatTime(c.get(Calendar.MINUTE));
		String mSecond = Utils.formatTime(c.get(Calendar.SECOND));

		String time = "[" + mYear + "-" + mMonth + "-" + mDay + " " + mHear
				+ ":" + mMinute + ":" + mSecond + "]";
		return time;
	}
	
}
